﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace GameWatcher
{
    public class FootballWatcherServiceReader
    {
        public FootballWatcherServiceReader()
        {
            //Default Constractoer
        }
        public static List<ClassXMLItems> ReadFeed(string url)
        {
            List<ClassXMLItems> rssFeedItems = new List<ClassXMLItems>();
            HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);
            int nExtractNumber = 0;
            string sOrignalText = string.Empty;
            string sSubstringString = string.Empty;
            using (DataSet rssData = new DataSet())
            {
                rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());
                foreach (DataRow objDataRow in rssData.Tables["item"].Rows)
                {
                    sOrignalText = objDataRow["title"].ToString();
                    string[] arrNumbersFromString = Regex.Split(sOrignalText, @"\D+");
                    foreach (string value in arrNumbersFromString)
                    {
                        if (!string.IsNullOrEmpty(value))
                        {
                            nExtractNumber = int.Parse(value);
                            sSubstringString = sSubstringString + nExtractNumber.ToString() + " - ";
                        }                        
                    }
                    if (!string.IsNullOrEmpty(sSubstringString))
                    {
                        sSubstringString = sSubstringString.Remove(sSubstringString.LastIndexOf("-"), 1);
                    }
                    rssFeedItems.Add(new ClassXMLItems
                    {
                        Title = Convert.ToString(sSubstringString),
                        Description = Convert.ToString(objDataRow["description"]),
                        Link = Convert.ToString(objDataRow["link"]),
                        pubDate = Convert.ToString(objDataRow["pubDate"])
                    });
                    sSubstringString = string.Empty;
                }
            }
            return rssFeedItems;
        }
    }
}
